//
//  CustomerAlertTextView.m
//  Elife
//
//  Created by 拓客营 on 2022/6/2.
//

#import "CustomerAlertTextView.h"
#import "YTTextView.h"

@interface CustomerAlertTextView()

@property (nonatomic, strong) YTTextView *textView;

@property (nonatomic, copy) void (^finishedBlock)(NSString *valueStr);

@end

@implementation CustomerAlertTextView

#pragma mark - LifeCycle
+(void)showWithTitle:(NSString *)title Placeholder:(NSString *)placeholder Complate:(void(^)(NSString *_Nullable valueStr))complate {
    
    CustomerAlertTextView *view = [[CustomerAlertTextView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) title:title placeholder:placeholder];
    view.finishedBlock = ^(NSString *valueStr) {
        complate(valueStr);
    };
    [kWindow addSubview:view];
    
}


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title placeholder:(NSString *)placeholder{
    self = [super initWithFrame:frame];
    if (self) {
        [self UIConfigWithTitle:title placeholder:placeholder];
    }
    return self;
}


#pragma mark - view layout
-(void)UIConfigWithTitle:(NSString *)title placeholder:(NSString *)placeholder {
    self.backgroundColor = kRGBA(0x000000, 0.3);
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 10;
    bgView.clipsToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.width.mas_equalTo(ScreenWidth-24);
        make.height.mas_offset(200);
    }];
    
    UILabel *titlaLab = [UILabel new];
    titlaLab.text = title;
    titlaLab.textColor = kRGB(0x333333);
    titlaLab.font = [UIFont boldSystemFontOfSize:16];
    titlaLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titlaLab];
    [titlaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.width.mas_offset(ScreenWidth-24);
        make.height.mas_equalTo(22);
    }];
    
    YTTextView *textView = [[YTTextView alloc]initWithFrame:CGRectZero placeholder:placeholder MaxLength:500];
    textView.hiddenCountLab = YES;
    textView.placeholderColor = kRGB(0x999999);
    textView.bgColor = UIColor.whiteColor;
    [bgView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 24, 50, 24));
    }];
    self.textView = textView;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [btn addTarget:self action:@selector(finishedBtnAction) forControlEvents:UIControlEventTouchUpInside];
    btn.expandEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [bgView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-15);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:ImageWithName(@"icon_alertText_close") forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    closeBtn.expandEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(bgView.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
}

#pragma mark - Event Response
-(void)finishedBtnAction {
    
    if (self.finishedBlock) {
        self.finishedBlock(self.textView.textValue);
    }
    [self removeFromSuperview];
}

-(void)closeBtnAction {
    [self removeFromSuperview];
}

#pragma mark - Delegate,DataSource

#pragma mark - Network

#pragma mark - Method

#pragma mark - Lazy

@end
